package ModelLayer;

import java.util.ArrayList;
import java.util.Date;

/**
 * RentalContainer is a container class for Rental.
 * 
 * @author group 4, dm82
 * @version 06-12-12
 */
public class RentalContainer
{
    private ArrayList<Rental>   rentalList;
    private static RentalContainer instance;
    /**
     * Constructor for objects of class RentalContainer
     */
    private RentalContainer()
    {
        rentalList = new ArrayList<Rental>();
    }
    
    /**
     * Checks if the tool is available in the given period
     * 
     * @param toolDescription. The tool description to be checked. As a ToolDescription
     * @param toStartDate. The starting date of the period. As a Date.
     * @param toEndDate. The ending date of the period. As a Date.
     * 
     * @return A true/false statement. If the tool is available it returns true, if it isn't it returns false.
     */
    public boolean checkToolAvailability(ToolDescription toolDescription, Date toStartDate, Date toEndDate){
         ToolDescription t = toolDescription;
          Date currentDate = toStartDate;
         
         while(currentDate.before(toEndDate) || currentDate.equals(toEndDate)){  
                   if(checkDayAvailability(currentDate, toolDescription)){
                       currentDate.setDate(currentDate.getDate()+1);
                    }
                   else{
                       return false;
                    }
            }

         return true;
    }
    
    /**
     * Checks if there is an available tool on a given day.
     * 
     * @param currentDate. The date to check
     * @param toolDescription. The tool description to be checked. As a ToolDescription
     * 
     * @return A boolean expression. If there is an available tool on the given day, it returns true. If not, it returns false
     */
    private boolean checkDayAvailability(Date currentDate, ToolDescription toolDescription){
        ToolDescription t = toolDescription;
        int rentedAmount = 0;
            for(int i = 0; rentalList.size()>i;i++){
                if(toolDescription.equals(rentalList.get(i).getToolDescription())){
                    if(currentDate.after(rentalList.get(i).getToStartDate()) || currentDate.equals(rentalList.get(i).getToStartDate())){
                        if(currentDate.before(rentalList.get(i).getToEndDate()) || currentDate.equals(rentalList.get(i).getToEndDate())){
                            rentedAmount++;
                        }
                    }
                }
            }
            if(rentedAmount<toolDescription.getAmount()){
                return true;
            }
            else{
                return false;
            }
    }
    
    /**
     * Returns a list of all the rentals with the given customer attached
     * 
     * @param customer. A customer whose rentals should be returned. As a Customer.
     * 
     * @return A list of Rentals with the customer attached. As an ArrayList.
     */
    public ArrayList<Rental> getCustomerRentalList(Customer customer){
        ArrayList<Rental> a = new ArrayList<Rental>();
        for(int i = 0; rentalList.size()>i;i++){
            if(customer.equals(rentalList.get(i).getCustomer())){
                a.add(rentalList.get(i));
            }
        }
        return a;
    }
    
    /**
     * Adds the given tool to the given rental.
     * 
     * @param tool. A tool to be added to a rental. As a Tool.
     * @param rental. A rental that should have a tool attached. As a Rental.
     */
    public void rentRental(long id, Date startDate, Tool tool){
        for(int i = 0; rentalList.size()>i;i++){
            if(rentalList.get(i).getID()==id)
            {
                rentalList.get(i).setStartDate(startDate);
                rentalList.get(i).setTool(tool);
                rentalList.get(i).setStatus(Rental.RENTED);
            }
        }
    }
    
    /**
     * This method returns an instance of this class in accordance with simpleton implementation.
     * 
     * @return  An instance of this class.
     */
    public static RentalContainer getInstance()
    {
        if(instance == (null))
        {
            instance = new RentalContainer();
        }
        return instance;
    }
    
    /**
     * This method adds a Rental.
     * 
     * @param   rental  A rental to be added. As a Rental.
     */
    public void addRental(Rental rental)
    {
        rentalList.add(rental);
    }
    
    /**
     * This method returns a list of all rentals
     * 
     * @return  The rental list
     */
    public ArrayList<Rental> getRentalList()
    {
        return rentalList;
    }
    
    /**
     * This method returns a rental.
     * 
     * @param   id  The id of the rental.
     * 
     * @return  The rental.
     */
    public Rental getRental(long id)
    {
        for(int i = 0; i<rentalList.size();i++)
        {
            if(rentalList.get(i).getID()==id)
            {
                return rentalList.get(i);
            }
        }
        return null;
    }
}

